<?php
/**
 * 系统管理模块所有的方法都在这个控制器
 * Created by swagger_zxd.
 * User: zxd
 * Date: 2018/1/17
 * Time: 10:05
 */

namespace app\admin\controller;


use app\index\controller\Base;
use think\Db;

class System extends Base
{
    /**
     * 运营列表
     * @return mixed
     */
    public function businessList()
    {
        if ($this->request->method()=="POST") {  //post请求
            //TODO

        } elseif ($this->request->method()=="GET") { //get请求
            try{
                $list=Db::connect('db2')->table('admin_salelogin')->where(['roletype'=>1])->select();

            }catch(\Exception $e){
                echo $e->getMessage();
            }
            return $this->fetch("businessList",['businessList'=>$list]);
        }
    }

    /**
     * 客服列表
     * @return mixed
     */
    public function customList()
    {
        if ($this->request->method()=="POST") {  //post请求

        } elseif ($this->request->method()=="GET") { //get请求
            try{
                $list=Db::connect('db2')->table('admin_salelogin')->where(['roletype'=>2])->select();

            }catch(\Exception $e){
                echo $e->getMessage();
            }
//            dump($list);
//            exit();
            return $this->fetch("customList",['customList'=>$list]);
        }
    }



    /**
     * 返回禁用和解禁
     * @return \think\response\Json
     */
    public function lockOrUnlock()
    {
        $data=$this->request->param();
        if ($data['type']==1) {
            try{
                $res=Db::connect('db2')->table('admin_salelogin')->where(['roletype'=>$data['roletype'],'id'=>$data['id']])->update(['State'=>1]);
                if ($res) {
                    $info=['msg'=>1];
                    return json($info);
                }else{
                    $info=['msg'=>0];
                    return json($info);
                }
            }catch(\Exception $e){
                echo $e->getMessage();
            }
        }elseif ($data['type']==2) {
            try{
                $res=Db::connect('db2')->table('admin_salelogin')->where(['roletype'=>$data['roletype'],'id'=>$data['id']])->update(['State'=>0]);
                if ($res) {
                    $info=['msg'=>1];
                    return json($info);
                }else{
                    $info=['msg'=>0];
                    return json($info);
                }
            }catch(\Exception $e){
                echo $e->getMessage();
            }
        }
    }


    /**
     * 添加客服
     * @return \think\response\Json
     */
    public function addBussinessOrKefu()
    {
        $data=$this->request->param();
        try{
            $insert=['NickName'=>$data['nickname'],'Accounts'=>$data['name'],'PassWord'=>md5($data['password']),'State'=>0,'RoleID'=>0,'RoleType'=>$data['roletype']];
            $res=Db::connect('db2')->table('admin_salelogin')->insert($insert);
            if ($res) {
                $info=['msg'=>1];
                return json($info);
            }else{
                $info=['msg'=>0];
                return json($info);
            }
        }catch(\Exception $e){
            echo $e->getMessage();
        }
    }


    public function changePassword()
    {
        $data=$this->request->param();
        try{
            $res=Db::connect('db2')->table('admin_salelogin')->where(['id'=>$data['id']])->update(['PassWord'=>md5($data['newpassword'])]);
            if ($res) {
                $info=['msg'=>1];
                return json($info);
            }else{
                $info=['msg'=>0];
                return json($info);
            }
        }catch(\Exception $e){
            echo $e->getMessage();
        }
    }

}